;; -*- lisp -*-

(require 'sawfish.wm)
(require 'sawfish.wm.frames)
(require 'sawfish.wm.menus)
(require 'sawfish.wm.misc)
(require 'sawfish.wm.windows)
(require 'sawfish.wm.util.prompt)
(require 'sawfish.wm.util.workarea)
(require 'sawfish.wm.util.compat)
(require 'sawfish.wm.commands.jump-or-exec)

;(require 'native-focus-transparency)
;(require 'smart-tile)
;(bind-keys global-keymap "Super-F2" 'tile-windows)

(defun tgl-no-frame (w)
   (when (window-frame w)
         (set-window-frame w nil)
	 (let* ((workarea (calculate-workarea #:window w))
		(posx (car workarea))
		(posy (cadr workarea))
		(dimx (- (caddr workarea) posx))
		(dimy (- (cadddr workarea) posy)))
	       (move-window-to w posx posy)
	(resize-window-to w dimx dimy))))

(defun tgl-frame (w)
    (when (not (window-frame w))
          (refresh-window w) ))

(add-hook 'window-maximized-hook tgl-no-frame)
(add-hook 'window-unmaximized-hook tgl-frame)

(bind-keys window-keymap
		   "Super-F4" 'delete-window
		   "Super-F5" 'destroy-window
		   "Super-RET" 'maximize-window-fullscreen-toggle
		   "Super-S-RET" 'iconify-window
		   "Super-!" '(send-to-workspace 1)
		   "Super-@" '(send-to-workspace 2)
		   "Super-Right" 'send-window-to-next-head
		   "Super-Left" 'send-window-to-previous-head
		   )

(bind-keys global-keymap
		   "Super-C-BS" '(system "sawfish-config &")
		   "Super-S-BS" 'restart
		   "Super-S-p" '(system "import -window root /tmp/screenshot-$(date +%Y%m%d%H%M).png")
		   "Print" '(system "import -window root /tmp/screenshot-$(date +%Y%m%d%H%M).png")
		   "Super-TAB" 'cycle-windows
		   "Super-j" 'next-workspace-window
		   "Super-k" 'previous-workspace-window
		   "Super-ESC" 'run-application
		   "Super-." 'next-workspace
		   "Super-0" 'delete-empty-workspaces
		   "Super-x" '(x-terminal-emulator "")
		   "Super-t" '(jump-or-exec "Term" "x-terminal-emulator -e tmux" nil #:match-class t)
		   "Super-e" '(jump-or-exec "Emacs" "/usr/bin/emacsclient -c -n" nil #:match-class t)
           "Super-q" '(jump-or-exec "Qutebrowser" "x-www-browser" nil #:match-class t)
           "Super-c" '(jump-or-exec "Qutebrowser" "x-www-browser" nil #:match-class t)
		   "Super-F1" '(system "timeout 1m sxiv -bq ~/.keys-sawfish.png &")
		   "Super-F2" '(system "bash -c \"gromit-mpx -a -o .5 --key F2\" &")
		   "Super-F12" '(system "x-terminal-emulator -e /usr/local/bin/stud &")
		   ;; audio controls
		   "XF86AudioMute" '(system "amixer set Master toggle && amixer set PCM toggle")
		   "XF86AudioLowerVolume" '(system "amixer set Master 3%- && amixer set PCM 3%-")
		   "XF86AudioRaiseVolume" '(system "amixer set Master 3%+ && amixer set PCM 3%+")
		   "XF86MonBrightnessDown" '(system "brightnessctl set 5%-")
		   "XF86MonBrightnessUp" '(system "brightnessctl set +5%")
		   "XF86WLAN" '(system "/bin/sleep 1"); disable XF86WLAN
		   )

;; 	   "XF86HomePage" '(system "/usr/bin/x-www-browser &")
;; 	   "XF86Mail" '(system "/usr/bin/x-www-browser --app=https://mail.google.com/mail/u/0/#inbox")
;; 	   "XF86Search" '(system "/usr/bin/x-www-browser &")
;; 	   "XF86Tools" '(system "/usr/bin/x-www-browser --app=http://music.163.com/#/my/")
;; 	"M-C-l" '(system "sudo /etc/acpi/sleepbtn.sh")
;; 	"XF86Sleep" '(system "sudo /etc/acpi/sleepbtn.sh")
